package boddaert.philippe.cashGame.site.priceminister;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import au.id.jericho.lib.html.Element;
import au.id.jericho.lib.html.Source;
import boddaert.philippe.cashGame.Site;
import boddaert.philippe.cashGame.parser.game.IWebGameParser;
import boddaert.philippe.cashGame.parser.game.WebGameParserFactory;
import boddaert.philippe.cashGame.site.WebGame;
import boddaert.philippe.cashGame.site.WebPage;

public class WebPageParser extends
		boddaert.philippe.cashGame.parser.page.WebPageParser {
	private final static Logger logger = Logger.getLogger(WebPageParser.class);

	@Override
	protected List<WebGame> extractGamesList(WebPage page) {
		Source source = new Source(page.getContent());
		List<Element> Elements = source.findAllElements("form");
		Element rootOfGamesList = Elements.get(0);
		List<WebGame> games = new ArrayList<WebGame>();

		for (Element element : Elements) {
			if ("frmCompare".equals(element.getAttributeValue("name")))
				rootOfGamesList = element;
		}
		List<Element> findAllElements = rootOfGamesList.findAllElements("div");

		for (Element element : findAllElements) {
			if (element.getAttributeValue("class") != null
					&& element.getAttributeValue("class").contains(
							"announce_blk")) {
				logger.debug("parse one game " + element);
				games.add(new WebGame(element));
			}
		}
		if (games.size() == 0)
			return null;

		return games;
	}

	public IWebGameParser getGameParser() {
		return WebGameParserFactory.get(Site.PRICEMINISTER);
	}

}
